package xnj.lazydog.btcontroller.MyViews;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;
import xnj.lazydog.btcontroller.R;



//// Page View ------------------------------------------------
//
//   该类用于方便地实现单个Activity中多页面视图的效果。一般针对水平左右滑动的页面。
//


public class PageView {

    Context context;
    private List<ViewGroup> buttonLayouts = new ArrayList<>();
    private List<View> viewLayouts =  new ArrayList<>();
    private List<ImageView> buttonImage = new ArrayList<>();
    private int [] imageGray = new int[]{R.drawable.tx_data_pack_a,R.drawable.rx_data_pack_a,R.drawable.set_data_pack_a};
    private int [] imageColor = new int[]{R.drawable.tx_data_pack_b,R.drawable.rx_data_pack_b,R.drawable.set_data_pack_b};
    private int lastTabIndex = -1;
    private int tabIndex;
    public PageView(Activity activity, int buttonParentID, int viewParentID,int []imageColor,int imageIndexOfButton)
    {
        context = activity;
        ViewGroup buttonParent = activity.findViewById(buttonParentID);
        ViewGroup viewParent = activity.findViewById(viewParentID);

        for(int i=0;i<buttonParent.getChildCount();i++) {
            buttonLayouts.add((ViewGroup) buttonParent.getChildAt(i));
            buttonImage.add((ImageView) buttonLayouts.get(i).getChildAt(imageIndexOfButton));
        }
        for(int i=0;i<viewParent.getChildCount();i++) {
            viewLayouts.add(viewParent.getChildAt(i));
        }
        this.imageColor = imageColor;
        setTab(buttonLayouts.get(0),true);
        for(int i=0;i<buttonLayouts.size();i++)
        {
            buttonLayouts.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTab(v,false);
                }
            });
        }
    }
    private void setTab(View v,boolean init)
    {

        int index = buttonLayouts.indexOf(v);
        tabIndex = index;
        if(init)
        {
            for(int i=0;i<buttonLayouts.size();i++)
            {
                if(v!=buttonLayouts.get(i)) {
                    viewLayouts.get(i).setVisibility(View.GONE);
                    buttonImage.get(i).setImageResource(imageGray[i]);
                }else {
                    viewLayouts.get(i).setVisibility(View.VISIBLE);
                    buttonImage.get(i).setImageResource(imageColor[i]);
                }
            }
            lastTabIndex = index;
            return;
        }
        if(index!=lastTabIndex) {
            buttonImage.get(index).setImageResource(imageColor[index]);
            viewLayouts.get(index).setVisibility(View.VISIBLE);
            if(lastTabIndex>=0) {
                buttonImage.get(lastTabIndex).setImageResource(imageGray[lastTabIndex]);
            }

            comeView(viewLayouts.get(index),index<lastTabIndex);
            if(lastTabIndex>=0)
                goView(viewLayouts.get(lastTabIndex),index<lastTabIndex);
        }
        lastTabIndex = index;
    }

    private void comeView(View v,boolean from_left)
    {
        if(from_left)
            v.startAnimation(AnimationUtils.loadAnimation(context,R.anim.come_from_left));
        else
            v.startAnimation(AnimationUtils.loadAnimation(context,R.anim.from_right));
    }
    private void goView(final View v,boolean to_right)
    {
        Animation animation;
        if(to_right)
            animation = AnimationUtils.loadAnimation(context,R.anim.to_right);
        else
            animation = AnimationUtils.loadAnimation(context,R.anim.to_left);

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                v.setVisibility(View.GONE);
            }
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        v.startAnimation(animation);
    }

    public int getPageIndex()
    {return tabIndex; }

}
